import EventMgr from "../../../Core/Event/EventMgr";
import Log from "../../../Core/Log/Log";
import TimerMgr from "../../../Core/Timer/TimerMgr";
import ProtoBufManager from "../../proto/ProtoBufManager";
import { game } from "../../proto/protoCmd";
import { AbstractModel } from "../socket/AbstractModel";
import { GameModels } from "../socket/GameModels";
import { SocketManager } from "../socket/SocketManager";
import { ModelPlayerInfo } from "./ModelPlayerInfo";

export class ModelGame extends AbstractModel {
    public Round: number;//回合
    public AckTime: number;//时间
    public AckUid: number;//进攻方uid
    public myAtkPos: game.IAttackSnapshot[] = [];
    public otherAtkPos: game.IAttackSnapshot[] = [];

    /**上报棋盘数据 */
    public send_ReqFinishLoadGame(PanelDate: game.PanelDate, clientpOS: string): void {
        const msg = ProtoBufManager.ins.game.ReqFinishLoadGame.create();
        msg.RoomId = GameModels.lobby.RoomId;
        msg.StartDate = PanelDate;
        const data = ProtoBufManager.ins.game.ReqFinishLoadGame.encode(msg).finish();
        this.send(3000, GameModels.lobby.GameRoute + `.ReqFinishLoadGame`, data);
    }
    /**退出房间 */
    public send_ReqExitGame(): void {
        if(!GameModels.lobby.RoomId){
            return;
        }
        const msg = ProtoBufManager.ins.game.ReqExitGame.create();
        msg.RoomId = GameModels.lobby.RoomId;
        const data = ProtoBufManager.ins.game.ReqExitGame.encode(msg).finish();
        this.send(3003, GameModels.lobby.GameRoute + `.ReqExitGame`, data);
    }
    //发送攻击
    public send_ReqAttack(AckPos: game.Coord): void {
        const msg = ProtoBufManager.ins.game.ReqAttack.create();
        msg.RoomId = GameModels.lobby.RoomId;
        msg.AckPos = AckPos;
        const data = ProtoBufManager.ins.game.ReqAttack.encode(msg).finish();
        Log.ins.log(1, `send_ReqAttack`, msg);
        this.send(3001, GameModels.lobby.GameRoute + `.ReqAttack`, data);
    }
    public send_ReqRoundFinish(): void {
        const msg = ProtoBufManager.ins.game.ReqRoundFinish.create();
        msg.RoomId = GameModels.lobby.RoomId;
        msg.Round = GameModels.game.Round;
        const data = ProtoBufManager.ins.game.ReqRoundFinish.encode(msg).finish();
        Log.ins.log(1, `send_ReqRoundFinish`);
        this.send(3002, GameModels.lobby.GameRoute + `.ReqRoundFinish`, data);
    }
    /**使用道具 */
    public send_ReqUseItem(itemId: number, pos?: game.ICoord): void {
        const msg = ProtoBufManager.ins.game.ReqUseItem.create();
        msg.RoomId = GameModels.lobby.RoomId;
        msg.ItemId = itemId;
        msg.UsePos = pos;
        const data = ProtoBufManager.ins.game.ReqUseItem.encode(msg).finish();
        Log.ins.log(1, `send_ReqUseItem:${itemId}`);
        this.send(3004, GameModels.lobby.GameRoute + `.ReqUseItem`, data);
    }
    protected registerInterestedProtocols(): void {
        this.reg(3000, this.onRespFinishLoadGame, `game`, `RespFinishLoadGame`);
        // this.reg(3001, this.onResAttack, `game`, `ResAttack`);
        this.reg(3002, this.onRespRoundFinish, `game`, `RespRoundFinish`);
        this.reg(3003, this.onRespExitGame, `game`, `RespExitGame`);
        this.reg(3004, this.onRespUseItem, `game`, `RespUseItem`);
    }

    public regRouteMap(): void {
        this.addRouteMap(GameModels.lobby.GameRoute + `.SyncGameRoundStart`, this.onSyncGameRoundStart, `game`, `SyncGameRoundStart`);
        this.addRouteMap(GameModels.lobby.GameRoute + `.SyncGameAck`, this.onSyncGameAck, `game`, `SyncGameAck`);
        this.addRouteMap(GameModels.lobby.GameRoute + `.SyncGameEnd`, this.onSyncGameEnd, `game`, `SyncGameEnd`);
        this.addRouteMap(GameModels.lobby.GameRoute + `.SyncUserEnterRoom`, this.onSyncUserEnterRoom, `game`, `SyncUserEnterRoom`);
        this.addRouteMap(GameModels.lobby.GameRoute + `.SyncUserExitRoom`, this.onSyncUserExitRoom, `game`, `SyncUserExitRoom`);
    }
    public removeRouteMap(): void {
        SocketManager.ins.unRouteHandler(GameModels.lobby.GameRoute + `.SyncGameRoundStart`);
        SocketManager.ins.unRouteHandler(GameModels.lobby.GameRoute + `.SyncGameAck`);
        SocketManager.ins.unRouteHandler(GameModels.lobby.GameRoute + `.SyncGameEnd`);
        SocketManager.ins.unRouteHandler(GameModels.lobby.GameRoute + `.SyncUserEnterRoom`);
        SocketManager.ins.unRouteHandler(GameModels.lobby.GameRoute + `.SyncUserExitRoom`);
    }
    private onRespUseItem(info: game.RespUseItem): void {

    }
    private onSyncUserExitRoom(info: game.SyncUserExitRoom): void {
        if (info.RoomId && GameModels.lobby.RoomId != info.RoomId) {
            Log.ins.log(1, `房间号不对,不处理,当前房间${GameModels.lobby.RoomId},收到房间${info.RoomId}`);
            return;
        }
        GameModels.lobby.OtherPlayer = null;
        EventMgr.emit(`event_SyncUserExitRoom`, info.Uid);

    }
    private onSyncUserEnterRoom(info: game.SyncUserEnterRoom): void {
        if (info.RoomId && GameModels.lobby.RoomId != info.RoomId) {
            Log.ins.log(1, `房间号不对,不处理,当前房间${GameModels.lobby.RoomId},收到房间${info.RoomId}`);
            return;
        }
        if (!GameModels.lobby.OtherPlayer) {
            GameModels.lobby.OtherPlayer = new ModelPlayerInfo();
        }
        GameModels.lobby.OtherPlayer.NickName = info.Player.NickName;
        GameModels.lobby.OtherPlayer.Avatar = info.Player.Avatar;
        GameModels.lobby.OtherPlayer.UserId = info.Player.Uid;
        GameModels.lobby.OtherPlayer.recordData = info.Player.Records;
        EventMgr.emit(`event_SyncUserEnterRoom`, info.Player);
    }
    private onSyncGameEnd(info: game.SyncGameEnd): void {
        if (info.RoomId && GameModels.lobby.RoomId != info.RoomId) {
            Log.ins.log(1, `房间号不对,不处理,当前房间${GameModels.lobby.RoomId},收到房间${info.RoomId}`);
            return;
        }
        Log.ins.log(1, `game.SyncGameEnd`, info);
        GameModels.game.AckTime = 0;
        GameModels.game.Round = 0;
        EventMgr.emit(`event_SyncGameEnd`, info);
    }
    private onRespExitGame(info: game.RespExitGame): void {
        GameModels.lobby.resetGame();
        GameModels.game.AckTime = 0;
        GameModels.game.Round = 0;
        GameModels.lobby.OtherPlayer = null;
        this.removeRouteMap();
    }
    private onRespRoundFinish(info: game.RespRoundFinish): void {

    }
    private onResAttack(info: game.RespAttack): void {

    }
    /**同步进攻 */
    private onSyncGameAck(info: game.SyncGameAck): void {
        if (info.RoomId && GameModels.lobby.RoomId != info.RoomId) {
            Log.ins.log(1, `房间号不对,不处理,当前房间${GameModels.lobby.RoomId},收到房间${info.RoomId}`);
            return;
        }
        if (info.AckUid == GameModels.info.playerInfo.UserId) {
            this.myAtkPos.push(...info.AckPos);
        } else {
            this.otherAtkPos.push(...info.AckPos);
        }
        Log.ins.log(1, `game.SyncGameAck`, info);
        EventMgr.emit(`event_SyncGameAck`, info.AckUid, info.AckPos, info.ItemId);
    }
    private onRespFinishLoadGame(info: game.RespFinishLoadGame): void {
        EventMgr.emit(`event_RespFinishLoadGame`);
    }
    private onSyncGameRoundStart(info: game.SyncGameRoundStart): void {
        if (info.RoomId && GameModels.lobby.RoomId != info.RoomId) {
            Log.ins.log(1, `房间号不对,不处理,当前房间${GameModels.lobby.RoomId},收到房间${info.RoomId}`);
            return;
        }
        Log.ins.log(1, `game.SyncGameRoundStart`, info);
        this.Round = info.Round || 0;
        this.AckTime = info.AckTime || null;
        this.AckUid = info.AckUid || null;
        EventMgr.emit(`event_SyncGameRoundStart`, info.UseItemIds);
    }
}